package com.ruoyi.web.controller.dongfang;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.question.domain.DfFile;
import com.ruoyi.question.domain.DfQuestion;
import com.ruoyi.question.domain.DfQuestionChild;
import com.ruoyi.question.service.IDfFileService;
import com.ruoyi.question.service.IDfQuestionChildService;
import com.ruoyi.question.service.IDfQuestionService;
import com.ruoyi.system.domain.SysRole;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.ruoyi.framework.util.ShiroUtils.getSysUser;

@Controller
@RequestMapping("/question/dfApprove")
public class DfApproveController extends BaseController {
    private String prefix = "question/dfApprove";

    @Autowired
    private IDfQuestionService dfQuestionService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private IDfQuestionChildService dfQuestionChildService;

    @Autowired
    private IDfFileService dfFileService;

    @RequiresPermissions("question:dfApprove:view")
    @GetMapping()
    public String dfApprove()
    {
        return prefix + "/dfApprove";
    }

    /**
     * 查询问题列表
     */
    @RequiresPermissions("question:dfApprove:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DfQuestion dfQuestion)
    {
        startPage();
        SysUser currentUser = getSysUser();
        SysRole userRole = currentUser.getRoles().get(0);
        if ("21".equals(getUserLevel(userRole.getRoleId())) ){//技术人员处理
            dfQuestion.setQuestionType("2");
        }else if("22".equals(getUserLevel(userRole.getRoleId()))){//技术人员解释
            dfQuestion.setQuestionType("3");
        }
        dfQuestion.setQuestionState("1");
        //dfQuestion.setQuestionExpert("0");
        List<DfQuestion> list = dfQuestionService.selectDfQuestionList(dfQuestion);
        return getDataTable(list);
    }

    /**
     * 查询已处理问题列表
     */
    @RequiresPermissions("question:dfApprove:list")
    @PostMapping("/examineList")
    @ResponseBody
    public TableDataInfo examineList(DfQuestion dfQuestion)
    {
        startPage();
        SysUser currentUser = getSysUser();
        SysRole userRole = currentUser.getRoles().get(0);
        if ("21".equals(getUserLevel(userRole.getRoleId()))){//研究院处理
            dfQuestion.setQuestionType("2");
        }else if("22".equals(getUserLevel(userRole.getRoleId()))){//研究院解释
            dfQuestion.setQuestionType("3");
        }
        //dfQuestion.setQuestionState("2");
        dfQuestion.setQuestionExpert("1");
        //dfQuestion.setQuestionState("9");
        List<DfQuestion> list = dfQuestionService.selectDfQuestionList(dfQuestion);
        return getDataTable(list);
    }
    /**
     * 解决问题
     */
    @Log(title = "解决问题", businessType = BusinessType.INSERT)
    @PostMapping("/passApprove/{id}")
    @ResponseBody
    public AjaxResult submitAgain(@PathVariable("id") Long id, DfQuestionChild dfQuestionChild)
    {
        //解决问题设置问题状态为已处理
        DfQuestion dfQuestion = dfQuestionService.selectDfQuestionById(id);
        dfQuestion.setQuestionState("2");
        dfQuestion.setQuestionExpert("1");
        dfQuestionChild.setQuestionId(dfQuestion.getId());
        dfQuestionChild.setQuestionType("2");
        dfQuestionService.updateDfQuestion(dfQuestion);
        dfQuestionChildService.insertDfQuestionChild(dfQuestionChild);
        return AjaxResult.success(dfQuestionChild.getId());
    }
    /**
     * 转出问题
     */
    @Log(title = "转出问题", businessType = BusinessType.INSERT)
    @PostMapping("/rollOut/{id}")
    @ResponseBody
    public AjaxResult rollOut(@PathVariable("id") Long id, DfQuestionChild dfQuestionChild)
    {
        //转出问题到专家
        DfQuestion dfQuestion = dfQuestionService.selectDfQuestionById(id);
        dfQuestion.setQuestionState("3");
        //dfQuestion.setQuestionExpert("1");
        //dfQuestionChild.setQuestionId(dfQuestion.getId());
        //dfQuestionChild.setQuestionType("2");

        //dfQuestionChildService.insertDfQuestionChild(dfQuestionChild);
        return toAjax(dfQuestionService.updateDfQuestion(dfQuestion));
    }
    /**
     * 查看问题
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Long id, ModelMap mmap)
    {
        DfQuestion dfQuestion = dfQuestionService.selectDfQuestionById(id);
        DfFile dfFile1 = new DfFile();
        dfFile1.setQuestionId(id);
        dfFile1.setFileSource("1");
        List<DfFile> files1 = dfFileService.selectDfFileList(dfFile1);
        for (int i = 0; i < files1.size(); i++) {
            DfFile file = files1.get(i);
            file.setFileId("http://211.149.205.33:18080/file/down/"+file.getFileId());
        }
        dfQuestion.setFiles(files1);
        mmap.put("dfQuestion", dfQuestion);

        SysUser currentUser = getSysUser();
        mmap.put("level",getUserLevel(currentUser.getDeptId()));
        DfQuestionChild dfQuestionChild = new DfQuestionChild();
        dfQuestionChild.setQuestionId(id);
        List<DfQuestionChild> list = dfQuestionChildService.selectDfQuestionChildList(dfQuestionChild);
        for (int i = 0; i <list.size() ; i++) {
            DfQuestionChild dfQuestionChild1 = list.get(i);
            DfFile dfFile = new DfFile();
            dfFile.setQuestionId(dfQuestionChild1.getId());
            dfFile.setFileSource("2");
            List<DfFile> files = dfFileService.selectDfFileList(dfFile);
            for (int j = 0; j < files.size(); j++) {
                DfFile file1 = files.get(j);
                file1.setFileId("http://211.149.205.33:18080/file/down/"+file1.getFileId());
            }
            dfQuestionChild1.setFiles(files);
        }
        mmap.put("dfQuestionChild",list);
        return prefix + "/view";
    }
    /**
     * 修改问题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        DfQuestion dfQuestion = dfQuestionService.selectDfQuestionById(id);
        DfFile dfFile1 = new DfFile();
        dfFile1.setQuestionId(id);
        dfFile1.setFileSource("1");
        List<DfFile> files1 = dfFileService.selectDfFileList(dfFile1);
        for (int i = 0; i < files1.size(); i++) {
            DfFile file = files1.get(i);
            file.setFileId("http://211.149.205.33:18080/file/down/"+file.getFileId());
        }
        dfQuestion.setFiles(files1);
        mmap.put("dfQuestion", dfQuestion);
        SysUser currentUser = getSysUser();
        mmap.put("level",getUserLevel(currentUser.getDeptId()));
        DfQuestionChild dfQuestionChild = new DfQuestionChild();
        dfQuestionChild.setQuestionId(id);
        List<DfQuestionChild> list = dfQuestionChildService.selectDfQuestionChildList(dfQuestionChild);
        for (int i = 0; i <list.size() ; i++) {
            DfQuestionChild dfQuestionChild1 = list.get(i);
            DfFile dfFile = new DfFile();
            dfFile.setQuestionId(dfQuestionChild1.getId());
            dfFile.setFileSource("2");
            List<DfFile> files = dfFileService.selectDfFileList(dfFile);
            for (int j = 0; j < files.size(); j++) {
                DfFile file1 = files.get(j);
                file1.setFileId("http://211.149.205.33:18080/file/down/"+file1.getFileId());
            }
            dfQuestionChild1.setFiles(files);
        }
        mmap.put("dfQuestionChild",list);


        return prefix + "/operate";
    }

    /**
     * 修改保存问题
     */
    @RequiresPermissions("question:dfApprove:edit")
    @Log(title = "问题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DfQuestion dfQuestion)
    {
        SysUser currentUser = getSysUser();

        return toAjax(dfQuestionService.updateDfQuestion(dfQuestion));
    }

    /**
     * 删除问题
     */
    @RequiresPermissions("question:dfApprove:remove")
    @Log(title = "问题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(dfQuestionService.deleteDfQuestionByIds(ids));
    }

}
